package src.main.CreationalPatterns.Builder;

import java.util.ArrayList;
import java.util.List;

public class GameCharacter {
    // 必选属性（final修饰确保不可变）
    private final String name;
    private final String profession;

    // 可选属性
    private String weapon;
    private String armor;
    private List<String> skills = new ArrayList<>();

    // 私有构造方法（强制使用Builder创建对象）
    private GameCharacter(Builder builder) {
        this.name = builder.name;
        this.profession = builder.profession;
        this.weapon = builder.weapon;
        this.armor = builder.armor;
        this.skills = builder.skills;
    }

    // 静态内部建造者类（实现链式调用）
    public static class Builder {
        // 必选属性（final修饰确保不可变）
        private final String name;
        private final String profession;

        // 可选属性
        private String weapon;
        private String armor;
        private List<String> skills = new ArrayList<>();

        // 必选参数构造方法
        public Builder(String name, String profession) {
            if (name == null || profession == null) {
                throw new IllegalArgumentException("名称或者职业不能为空");
            }
            this.name = name;
            this.profession = profession;
        }

        // 可选参数设置方法（返回Builder实现链式调用）
        public Builder weapon(String weapon) {
            // 职业装备校验（如战士不能装备法杖）
            if ("战士".equals(this.profession) && !"大剑".equals(weapon)) {
                throw new IllegalArgumentException("战士职业不匹配");
            }
            this.weapon = weapon;
            return this;
        }

        public Builder armor(String armor) {
            this.armor = armor;
            return this;
        }

        public Builder skill(String skill) {
            this.skills.add(skill);
            return this;
        }

        public GameCharacter build() {
            if (skills.isEmpty()) {
                System.out.println("角色未装备技能");
            }
            return new GameCharacter(this);
        }
    }

    // 显示角色信息
    public void display() {
        System.out.println("\n=== 角色创建成功 ===");
        System.out.println("姓名：" + name);
        System.out.println("职业：" + profession);
        System.out.println("武器：" + (weapon != null ? weapon : "无"));
        System.out.println("护甲：" + (armor != null ? armor : "无"));
        System.out.println("技能：" + (skills.isEmpty() ? "无" : String.join(", ", skills)));
    }
}


